﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Gman.Entity.Entities;
using Gman.WebApp.ServiceControllers;
using Gman.WebApp.Models;
using System.Web.Caching;
using Gman.WebApp.Controllers.Base;
using System.Web.UI;

namespace Gman.WebApp.Controllers
{
    public class DocumentoController : BaseController
    {
        //
        // GET: /Documento/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Documento/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Documento/Create

        public ActionResult Create()
        {
            DocumentosBE documento = new DocumentosBE();
            var model = new DocumentoModel(documento);

            model.Action = "search";
            model.ToolBar = new ToolBarModel();

            return View(model);



            return View();
        } 

        //
        // POST: /Documento/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Documento/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Documento/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Documento/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Documento/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }




        public DocumentoModel GetDocumentoModel(Guid id)
        {
            var cache = new CacheProvider();

            string key = id.ToString();
            var existingModel = cache.Get(key);
            if (existingModel == null)
            {
                existingModel = new DocumentoModel(new DocumentosBE());
                cache.Set(key, existingModel);
            }
            return (DocumentoModel)existingModel;
        }

        public ActionResult UpdateField(string fieldName, string fieldValue, Guid id, string setterFormatOptions)
        {
            DocumentoModel model = GetDocumentoModel(id);
            SetterFormatOptions setterFormatOptionsEnum = SetterFormatOptionsHelper.Parse(setterFormatOptions);
            List<KeyValuePair<string, string>> validationList = new List<KeyValuePair<string, string>>();
            List<KeyValuePair<string, string>> propertyChangeList = new List<KeyValuePair<string, string>>();

            var setter = new PropertySetter<DocumentosBE>.SetterOptions();
            setter.objectToSet = model.Documento;
            setter.property = fieldName;
            setter.val = fieldValue;
            setter.SetterFormatOptions = setterFormatOptionsEnum;
            setter.validationList = validationList;

            object valueWasSet = PropertySetter<DocumentosBE>.SetFinalObject(setter);

            if (valueWasSet == null)
            {
                propertyChangeList.Add(new KeyValuePair<string, string>(fieldName, null));
            }
            else
            {
                if (setter.SetterFormatOptions != SetterFormatOptions.None)
                {
                    propertyChangeList.Add(new KeyValuePair<string, string>(fieldName, SetterFormatOptionsHelper.FormatAsString(valueWasSet, setter.SetterFormatOptions)));
                }
            }

            validationList.AddRange(new DocumentoModelValidator().Validate(model));

            return Json
                (new
                {
                    result = true,
                    PropertyValidationList = validationList,
                    PropertyChangeList = propertyChangeList
                }
                );
        }

        public ActionResult GetValidation(Guid id)
        {
            DocumentoModel model = GetDocumentoModel(id);
            List<KeyValuePair<string, string>> validationList = new List<KeyValuePair<string, string>>();
            List<KeyValuePair<string, string>> propertyChangeList = new List<KeyValuePair<string, string>>();

            validationList.AddRange(new DocumentoModelValidator().Validate(model));

            return Json(new
            {
                PropertyValidationList = validationList,
                PropertyVisibilityList = propertyChangeList
            });
        }






    }
}
